<?php

/* 店铺控制器 */
class Store_billApp extends BackendApp
{
    var $_storeBill_mod;
    var $_storeBillDetail_mod;
    var $_store_mod;
    function __construct()
    {
        $this->StoreApp();
    }

    function StoreApp()
    {
        parent::__construct();
        $this->_storeBill_mod =& m('store_bill');
        $this->_store_mod =& m('store');
        $this->_storeBillDetail_mod =& m('store_bill_detail');
    }


    function index(){
        $page = $this->_get_page();
        $conditions = $this->_get_query_conditions(array(
            array(
                'field' => 'status',
                'equal' => '=',
                'type'  => 'numeric',
            ),
            array(
                'field' => 'store_bill.account_no',       //按用户名,店铺名,支付方式名称进行搜索
                'equal' => 'LIKE',
                'name'  => 'account_no',
            ),
        ));
        if($_GET['store_id']){
            $conditions .= ' AND store_bill.store_id = '.$_GET['store_id'];
            $this->assign('store_id',$_GET['store_id']);
        }

        if($_GET['add_time_from']){
            $from_time = strtotime($_GET['add_time_from']);
            $conditions = ' AND start_time >='.$from_time;
            if($_GET['add_time_to']){
                $to_time =strtotime($_GET['add_time_to']);
                $conditions = ' AND start_time >='.$from_time.' AND end_time <='.$to_time;
            }
        }elseif($_GET['add_time_to']){
            $to_time =strtotime($_GET['add_time_to']);
            $conditions = ' AND end_time <='.$to_time;
        }
        $this->assign('add_time_from',$_GET['add_time_from']);
        $this->assign('add_time_to',$_GET['add_time_to']);
        $res = $this->_storeBill_mod->find(array(
            'fields' => 'store_bill.*,store.store_name',
            'join' => 'has_store_commission',
            'conditions'    => '1=1 ' . $conditions,
            'limit' => $page['limit'],
            'count' => true,
            'order' => "start_time desc"
        ));
        foreach($res as $k=>$v){
            //定义状态
            $res[$k]['status_value'] = Store_billModel::$statusMap[$v['status']];
        }
        //查询所有店铺
        $storeList = $this->_store_mod->find(array("conditions"=>"store_type = 'shops'"));

        $this->assign('store_bill_status_list', array(
            1  =>  Store_billModel::$statusMap[1],
            2  =>  Store_billModel::$statusMap[2],
            3  =>  Store_billModel::$statusMap[3],
        ));
        $this->assign('storeList', $storeList);
        $page['item_count'] = $this->_storeBill_mod->getCount();   //获取统计的数据

        $this->_format_page($page);
        $this->assign('res', $res);
        $this->assign('filtered', $conditions? 1 : 0); //是否有查询条件
        $this->import_resource(array('script' => 'inline_edit.js,jquery.ui/jquery.ui.js,jquery.ui/i18n/' . i18n_code() . '.js',
            'style'=> 'jquery.ui/themes/ui-lightness/jquery.ui.css'));
        $this->assign('page_info', $page);
        $this->display('store_bill.index.html');
    }

    //审核通过账单审核
    function operation(){
        $id = $_POST['id'];
        $msg = '参数错误';
        if($id && is_numeric($id)){
            $info = $this->_storeBill_mod->get(array("conditions"=>"id = ".$id));
            $msg = '信息错误';
            if(!empty($info)){
                $msg = '账单状态错误';
                if($info['status'] == 1){
                    $saveData['confirmation_time'] = time();
                    $saveData['status'] = 2;
                    $save = $this->_storeBill_mod->edit($id,$saveData);
                    $msg = '操作失败';
                    if($save){
                        $msg = '操作成功';
                    }
                }
            }
        }
        echo json_encode(array('msg'=>$msg));
        exit();
    }
    //批量审核通过
    function operations(){
        $id = $_GET['id']?trim($_GET['id']):'';

        if(!$id){
            $this->show_warning('参数错误');
            return false;
        }
        $ids = explode(',',$id);
        if(!empty($ids)){
            $time = time();
            foreach($ids as $k=>$v){
                $info = $this->_storeBill_mod->get(array("conditions"=>"id = ".$v));
                if($info['status'] == 1){
                    $saveData['status'] = 2;
                    $saveData['confirmation_time'] = $time;
                    $save = $this->_storeBill_mod->edit($v,$saveData);
                }else{
                    continue;
                }
            }
        }
        $this->show_message('操作成功！', 'back_list', 'index.php?app=store_bill&act=index&page=' . $_GET['ret_page']);
    }



    //导出功能
    function excel(){
        $id = $_GET['id']?trim($_GET['id']):$_GET['id'];
        if(!$id){
            $this->show_warning('参数错误');
            return false;
        }
        $res = $this->_storeBillDetail_mod->find(array('conditions'=>'store_account_id = '.$id));
        if(empty($res)){
            $this->show_warning('数据为空！');
            return false;
        }
        $info = $this->_storeBill_mod->get(array("conditions"=>"id = ". $id));
        $storeInfo = $this->_store_mod->get(array("conditions"=>"store_id = ". $info['store_id']));

        $to_charset = (CHARSET == 'utf-8') ? substr(LANG, 0, 2) == 'sc' ? 'gbk' : 'big5' : '';
        $num = 0;
        $data[$num]['order_sn'] = '订单号';
        $data[$num]['order_pay_time_value'] = '订单收款日期';
        $data[$num]['goods_id'] = '产品编码';
        $data[$num]['goods_name'] = '产品名称';
        $data[$num]['sku'] = 'sku';
        $data[$num]['num'] = '数量';
        $data[$num]['price'] = '产品单价';
        $data[$num]['price_amount'] = '总金额(不含税不运费)';
        $data[$num]['order_finish_time_value'] = '订单成交日期';
        $data[$num]['commission_proportion_value'] = '服务费%';
        $data[$num]['sign'] = '正负值';
        $data[$num]['commission'] = '服务费';
        $data[$num]['stock_style'] = '销售方式';
        $data[$num]['store_name'] = '店铺名称';
        $data[$num]['serial_number'] = '业务员代码';
        //格式化type 、status  字段
        foreach($res as $k=>$v){
            $res[$k]['order_pay_time_value'] = '未付款';
            if($v['order_pay_time'] > 0){
                $res[$k]['order_pay_time_value'] = date('Y-m-d H:i:s',$v['order_pay_time']);
            }
            $res[$k]['order_finish_time_value'] = '未完成';
            if($v['order_finish_time'] > 0){
                $res[$k]['order_finish_time_value'] = date('Y-m-d H:i:s',$v['order_finish_time']);
            }

            //格式化账单商品信息
            $dataArr = json_decode($v['data'],true);

            $res[$k]['commission_finish_time_value'] = date('Y-m-d H:i:s',$v['commission_finish_time']);
            $dataArr['commission_proportion_value'] = $dataArr['commission_proportion'] * 100;

            $data[$num+1]['order_sn'] = $dataArr['order_sn'];
            $data[$num+1]['order_pay_time_value'] = $res[$k]['order_pay_time_value'];
            $data[$num+1]['goods_id'] = $dataArr['goods_id'];
            $data[$num+1]['goods_name'] = $dataArr['goods_name'];
            $data[$num+1]['sku'] = $dataArr['sku'];
            $data[$num+1]['num'] = $dataArr['num'];
            $data[$num+1]['price'] = '￥'.$dataArr['price'];
            $data[$num+1]['price_amount'] = '￥'.$dataArr['price_amount'];
            $data[$num+1]['order_finish_time_value'] = $v['order_finish_time_value'];
            $data[$num+1]['commission_proportion_value'] = $dataArr['commission_proportion_value'].'%';
            $data[$num+1]['sign'] = $v['sign'];
            $data[$num+1]['commission'] = $dataArr['commission'];
            $data[$num+1]['stock_style'] = $dataArr['stock_style'];
            $data[$num+1]['store_name'] = $v['store_name'];
            $data[$num+1]['serial_number'] = $v['serial_number'];
            $num++;
        }
        $this->export_to_csv($data, date('Y-m-d H:i:s').'店铺'.$storeInfo['store_name'].'账单信息', $to_charset);
        exit();
    }

    //打款
    function makeMoney(){
        echo json_encode(array("msg"=>"功能暂未开通"));
        exit();
        $res = $this->_commission_account_mod->makeModel();
    }



    //查看账单详情
    function detail(){
        $id = $_POST['id'];
        $code = -1;
        $msg = '参数错误';
        $data = array();
        if($id && is_numeric($id)){
            $msg = '账单信息错误';
            $store_billInfo = $this->_storeBill_mod->get(array("conditions"=>"id = ".$id));
            if(!empty($store_billInfo)){
                $msg = '账单信息为空';
                $store_bill_detailInfo = $this->_storeBillDetail_mod->find(
                    array("conditions"=>"store_account_id = ".$id)
                );
                if(!empty($store_bill_detailInfo)){
                    foreach($store_bill_detailInfo as $k=>$v){
                        //格式化账单商品信息
                        $dataArr = json_decode($v['data'],true);

                        $store_bill_detailInfo[$k]['order_pay_time_value'] = '未付款';
                        if($v['order_pay_time'] > 0){
                            $store_bill_detailInfo[$k]['order_pay_time_value'] = date('Y-m-d H:i:s',$v['order_pay_time']);
                        }
                        $store_bill_detailInfo[$k]['order_finish_time_value'] = '未完成';
                        if($v['order_finish_time'] > 0){
                            $store_bill_detailInfo[$k]['order_finish_time_value'] = date('Y-m-d H:i:s',$v['order_finish_time']);
                        }
                        $store_bill_detailInfo[$k]['commission_finish_time_value'] = date('Y-m-d H:i:s',$v['commission_finish_time']);
                        $store_bill_detailInfo[$k]['commission_proportion_value'] = $dataArr['commission_proportion'] * 100;
                        $store_bill_detailInfo[$k]['data'] = $dataArr;
                    }
                    $code = 1;
                    $msg = '查询成功';
                    $data = $store_bill_detailInfo;
                }
            }
        }
        echo json_encode(array('code'=>$code,'msg'=>$msg,'data'=>$data));
        exit();
    }



}